<%@page contentType="text/html;charset=utf-8"%>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
   	<link rel="stylesheet" href="http://cdn.bootcss.com/bootstrap/3.3.0/css/bootstrap.min.css">
    <!-- <script src="../a/js/jquery-3.4.1.min.js"></script> -->
    <script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
    <script src="http://cdn.bootcss.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
    <title>登录</title>
<style>
.back{
    background-image: url(../img/loginbackground.jpg)
}
</style>
</head>
<body>
<body class="back">
<div style="width:320px;height:400px;margin:auto;margin-top:20%">
    <h1>登录</h1>
    <p>用户名：
        <input id="username" type="text" name="username">
    </p>
    <p>密码：
        <input id="password" type="text" name="password">
    </p><br>
    <button style="margin-left:30px" type="button" onclick="check()">登录</button>
    <button style="margin-left:80px" type="button" onclick="">注册</button><br>
</div>
<script type="text/javascript">
function check(){
    var url = 'http://localhost:8080/EnterpriseWebsite/login/login';
    var username=$("#username").val();
	var password=$("#password").val();
    if(username==""){
		alert("账号不能为空！");
		$('#username').focus();//若用户名为空，直接将光标放在用户名输入框
	}
	else if(password==""){
		alert("密码不能为空！");
		$('#password').focus();//判断输入框是否有值
		
	}
	else {
		var s = {
				"username":username,//数据打包
				"password":password,	
		};
		$.ajax({
			type:"post",//post方式传输，比get更加密安全
			url:url,//请求servlet地址
			data:s,//打包data
		    dataType: "JSON",//json传输方式
			cache:false,
			async:false,
		    success: function (data ,textStatus, jqXHR)
		    {
		    	if( "${data.username}" != null){
		    		alert("登录成功");
		    		window.location.href="http://localhost:8080/EnterpriseWebsite/a/home.jsp";//登录成功进入该网站
		    	}else{
		    		alert("登录失败");
		    	}
		    },
		    error:function (XMLHttpRequest, textStatus, errorThrown) {      
		      	//请求失败之后执行这个
		    	alert("发生未知错误，请求失败1");
		    }
		})
	}}
	
</script>



</body>
</html>